$(function(){
	var userGrid = $("#sign");
	var i=0;
	//初始化
	userGrid.datagrid({
		url:'student/signList',
		fit:true,
		border:false,
		fitColumns:true,
		rownumbers:true,
		emptyMsg:"<img src='public/images/ty.png'/>",
		pagination : true,// 是否显示分页栏
		columns : [ [ {
			field : 'emp',
			title : '咨询师',
			align : 'center',
			width : 90,
			formatter: function (value, rec) {
		         return rec.emp.name;
		     }
		},  {
			field : 'student',
			title : '学生',
			align : 'center',
			width : 90,
			formatter: function (value, rec) {
		         return rec.student.name;
		     }
		}, {
			field : 'rec.image.path',
			title : '签约凭据',
			align : 'center',
			width : 90,
			formatter: function (value, rec, row, index) {
				var bts = [];
				bts.push('<a data-id="'+rec.image.id+'" data-index="'+index+'"  target=true  class="imgyl" href="student/ylimg/'+rec.image.path+'">预览</a>');
				bts.push('<img id="yl'+rec.image.id+'" style="width:255px;height:175px; display: none;position: absolute;top: 49px;right: 295px;" src="student/ylimg/'+rec.image.path+'" />');
				return bts.join('');
		     }
		} ,{
			field : 'signTime',
			title : '签约时间',
			align : 'center',
			width : 90,
			formatter : function(val){
				var newTime = new Date(val);
				var year = newTime.getFullYear();  //获取年
		        var month = newTime.getMonth() + 1;    //获取月
		        var day = newTime.getDate(); //获取日
		        var hours = newTime.getHours(); 
		        var minutes = newTime.getMinutes();
		        var time = year + "-" + month + "-" + day;
				return time;
			} 
		}, {
			field : 'ac',
			title : '操作',
			width : 100,
			align : 'center',
			formatter:function(value,row,index){
				var bts = [];
				bts.push('<a data-index="'+index+'" class="ctr details">详情</a>');
				return bts.join('');
			}
		} ] ]
	});
	var stuName = $("#signstuName");
	var empName = $("#signempName");
	
	//检索按钮时间
	$("#signSearch").on("click",function(){
		userGrid.datagrid("load",{
			student:stuName.val(),
			emp:empName.val()
		});
	});
	var form = $("#signSearchForm");

	$("#signReset").on('click', function() {
		form.form('clear');
		// 清除查询参数
		userGrid.datagrid("load", {});
	});
	var gridPanel = userGrid.datagrid("getPanel");
	gridPanel.on("mouseenter", "a.imgyl", function() {
		var index = this.dataset.index;
		var id = this.dataset.id;
		var yl = $("#yl"+id);
		yl.css('display','block'); 
	
	}).on("mouseout", "a.imgyl", function() {
		var index = this.dataset.index;
		var id = this.dataset.id;
		var yl = $("#yl"+id);
		yl.css('display','none'); 

	}).on("click", "a.details", function() {
	var index = this.dataset.index;
	// 如果只有下标，没有id的情况，要获取id
	var rows = userGrid.datagrid("getRows");
	// 同下标获取对应行的数据对象
	var id = rows[index].id;
	details(id);
	});
	
	/**
	 * 学生详情表单
	 */
	function details(id){
		// 创建窗口
		var dialog = $("<div/>").dialog({
			href : 'student/sign_student_details' + (id ? ("/" + id) : ""),
			title : "学生详情",
			height : 380,
			width : 600,
			modal : true,
			onClose : function() {
				// 窗口关闭的同时销毁此窗口
				$(this).dialog("destroy");
			}
		});
	}
});